package com.epfresh.supplier.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView;

import com.epfresh.api.entity.RequestEntitySupply;
import com.epfresh.api.entity.ResponseEntity;
import com.epfresh.api.entity.StatusEntity;
import com.epfresh.api.global.BaseApplication;
import com.epfresh.api.global.BaseActivity;
import com.epfresh.api.http.OnRequestListener;
import com.epfresh.supplier.R;
import com.epfresh.api.widget.gridtext.GridPasswordView;
import com.epfresh.api.utils.T;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.Map;

public class OrderEnsureActivity extends BaseActivity {
    private TextView tvSure;
    private GridPasswordView gpv;

    private TextView tvErrorHint;
    String orderId, inspectCode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_ensure);
        tvSure = (TextView) findViewById(R.id.tv_sure);
        tvSure.setOnClickListener(this);
        tvErrorHint = (TextView) findViewById(R.id.tv_error_hint);
        orderId = getIntent().getStringExtra("id");
        if(getIntent().getBooleanExtra("agentFlag",false)){
            cmdOrderCheck = "pm-order/baseorder/order/checkInspectCode";
        }
        initView();
        InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(getCurrentFocus(), InputMethodManager.SHOW_FORCED);
    }

    private void initView() {
        ivBack.setVisibility(View.VISIBLE);
        tvBack.setVisibility(View.VISIBLE);
        basetitle.setText("交货码验证");
        gpv = (GridPasswordView) findViewById(R.id.ll_number);
        gpv.setPasswordVisibility(true);
        gpv.setOnPasswordChangedListener(new GridPasswordView.OnPasswordChangedListener() {
            @Override
            public void onTextChanged(String psw) {
                inspectCode = psw;
            }

            @Override
            public void onInputFinish(String psw) {
                inspectCode = psw;
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_sure:
                //T.toast("提交或确认+orderid+inspectCode"+orderId+inspectCode);
                if (inspectCode == null || inspectCode.length() < 6) {
                    T.toast("请输入六位交货码");
                    return;
                }
                post();
                break;
            case R.id.baseback:
                InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
                if (imm.isActive()) {
                    imm.hideSoftInputFromWindow(view.getApplicationWindowToken(), 0);
                }
                finish();
                break;
            default:
                break;
        }
    }

    String cmdOrderCheck = com.epfresh.api.constant.AppSupplyRouterConstant.cmd_order_supplier_order_checkInspectCode;

    private void post() {
        RequestEntitySupply<Map<String, Object>> data = new RequestEntitySupply<>();
        data.setCmd(cmdOrderCheck);
        Map<String, Object> map = new HashMap<>();
        map.put("supplyId", getUser().getAccountId());
        map.put("orderId", orderId);
        map.put("inspectCode", inspectCode);
        data.setParameters(map);
        request(data, cmdOrderCheck, onRequestCodeListener);
    }


    OnRequestListener<StatusEntity> onRequestCodeListener = new OnRequestListener<StatusEntity>() {
        @Override
        public StatusEntity jsonToObj(String responseStr) {
            return new Gson().fromJson(responseStr, StatusEntity.class);
        }

        @Override
        public void onFail(int failCode, Object arg, Object tag) {
            hideProgressDialog();

        }

        @Override
        public void onResponseError(int failCode, Object arg, Object tag) {
            hideProgressDialog();
        }

        @Override
        public void onStart(Object tag) {
            showProgressDialog();
        }

        @Override
        public void onResponse(ResponseEntity<StatusEntity> response, Object tag) {
            hideProgressDialog();
            StatusEntity responseElement = response.getResponseElement();
            if (responseElement.isSuccess()) {
                InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
                toast(BaseApplication.getDoc().getInspectCodeRight());
                tvErrorHint.setVisibility(View.GONE);
/*                if (getIntent().getBooleanExtra("listFlag", false)) {
                    finish();
                    return;
                }*/
                Intent back = new Intent();
                setResult(0x002, back);
                finish();
            } else {
                toast(responseElement.getMsg());
                tvErrorHint.setVisibility(View.VISIBLE);
            }
            return;
        }
    };
}
